<?php

namespace app\admin\controller\store;

use Throwable;
use ba\Random;
use app\common\controller\Backend;
use app\admin\model\Shop as ShopModel;
use app\admin\model\ShopSetting as ss;
use app\admin\model\Config;
class Shop extends Backend
{
 	
 
	public function subShopSettings(): void{
	     $data = input("post.data");
	     foreach($data as $k=>$v){
	     		$find = Config::where(['name'=>$k,'group'=>'wx_config','shop_id'=>$this->store_id])->find();
	     		if($find){
	     			   Config::where(['name'=>$k,'group'=>'wx_config','shop_id'=>$this->store_id])->update(['value'=>$v]);
	     		}
	     }
	     $this->success('ok',[
		  	'list'=>[]
		  ]);
	}


	function getShopSettings(){
			$res = Config::where(['shop_id'=>$this->store_id])->select()->toArray();
			foreach($res as $k=>$v){
				  $arr[$v['name']] = $v['value'];
			}

			 $this->success('ok',[
		  	   'data'=>$arr??[]
		     ]);
	}

	function myshopindex(){
		  $admin = $this->auth->getInfo();
		  $data = ShopModel::alias("s")->join("admin a","s.admin_id=a.id","LEFT")->field("s.*,a.username")->where("s.admin_id",$admin['id'])->select();
		  foreach($data as &$v){
		  	  $v['btns'] = [$v['status'],$v['sid']];
		  	  $v['current'] = $this->store_id == $v['sid'] ? 1 :0;
		  }
		  $this->success('ok',[
		  	'list'=>$data
		  ]);
	}
	function shopStatus(){

			$admin = $this->auth->getInfo();
			$id = input("get.sid");
			$s = input("get.os");
			$up = ShopModel::where(['admin_id'=>$admin['id'],'sid'=>$id])->update(['status'=>$s]);
			if($up){
				 $this->success("ok");
			}else{
				 $this->error("失败");
			}
	}

	function subAbout(){
  		 $type = input("post.type");
		 $data = input("post.data");
		 switch ($type) {
		 	case 'del':
		 		$dd = ss::where(['id'=>$data,'type'=>'about','shop_id'=>$this->store_id])->delete();
		 		if($dd){
		 			 $this->success("ok");
		 		}else{
		 			 $this->error("栏目已删除或删除错误"); 
		 		}
		 		break;
		 	case 'addNew':
		 	    $data['type'] = 'about';
		 	    $data['shop_id'] = $this->store_id;
		 	    $find = ss::where(['name'=>$data['name'],'type'=>'about','shop_id'=>$this->store_id])->find();
		 	    if($find){
		 	    	 $this->error("字段已存在，请更换!"); 
		 	    }
		 		ss::insert($data);
		 		break;
		 	
		 	default:
		 		foreach($data as $k=>$v){
			 		$find = ss::where(['name'=>$v['name'],'type'=>'about','shop_id'=>$this->store_id])->find();
			 		if($find){
			 				ss::where(['name'=>$v['name'],'type'=>'about','shop_id'=>$this->store_id])->update(['settings'=>$v['settings']]);
			 		}else{
			 				ss::insert(['name'=>$v['name'],'type'=>'about','title'=>$arr[$k],'settings'=>$v,'shop_id'=>$this->store_id]);
			 				
			 		}
				 }
		 		break;
		 }
		 
		 $this->success("ok");
	}

	function getAboutSettings(){
		 $res = ss::where(['type'=>'about','shop_id'=>$this->store_id])->select();
		 $this->success("ok",$res);
	}
}